/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test;

import java.util.Iterator;
import java.util.Vector;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import tavernacon.DataValue;

/**
 *
 * @author rory
 */
public class TavernaServer {

    /*public static String[] getJobs(PortletSession session) {

        String[] jobs = null;

        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse("http://rpc212.cs.man.ac.uk:8081/taverna-server-0.2.1/rest/jobs");
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for workflows:
            //XPathExpression WFExpr = xpath.compile("//dataMap/entry/value/list/value/text()");
            XPathExpression jobIDExpr = xpath.compile("//Jobs/job/id/text()");
            Object jobIDResult = jobIDExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList jobIDNodeList = (NodeList) jobIDResult;

            XPathExpression jobWorkflowExpr = xpath.compile("//workflow/text()");
            Object jobWorkflowResult = jobWorkflowExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList jobWorkflowNodeList = (NodeList) jobWorkflowResult;

            XPathExpression jobStatusExpr = xpath.compile("//status/text()");
            Object jobStatusResult = jobStatusExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList jobStatusNodeList = (NodeList) jobStatusResult;



            jobs = new String[jobIDNodeList.getLength()];

            for (int i = 0; i < jobIDNodeList.getLength(); i++) {

                String job = (String) (jobIDNodeList.item(i).getNodeValue());

                jobs[i] = job;

                //results[i] = jobIDNodeList.item(i).getNodeValue();
            }

        } catch (Exception e) {
        }

        //System.out.println(jobs);
        return jobs;

    }*/

    public static Vector getOutputs(PortletSession session) {

        String jobID = (String) session.getAttribute("selectedJobID");
        System.out.println("Selected job: "+jobID);
        final Vector outputs = new Vector();

        try {

            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();

            Document jobDoc = builder.parse("http://rpc212.cs.man.ac.uk:8081/taverna-server-0.2.1/rest/jobs/" + jobID);

            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();

            XPathExpression statusExpr = xpath.compile("//Job/status/text()");
            Object statusResult = statusExpr.evaluate(jobDoc, XPathConstants.NODESET);
            NodeList statusNodeList = (NodeList) statusResult;

            String status = statusNodeList.item(0).getNodeValue();
            session.setAttribute("status", status);

            XPathExpression outputIDExpr = xpath.compile("//Job/outputs/text()");
            Object outputIDResult = outputIDExpr.evaluate(jobDoc, XPathConstants.NODESET);
            NodeList outputIDNodeList = (NodeList) outputIDResult;

            String outputID = outputIDNodeList.item(0).getNodeValue();
            System.out.println("Output data ID: " + outputID);

            Document outputsDoc = builder.parse("http://rpc212.cs.man.ac.uk:8081/taverna-server-0.2.1/rest/data/" + outputID);

            XPathExpression outputExpr = xpath.compile("//entry/value//value/text()");
            Object outputResult = outputExpr.evaluate(outputsDoc, XPathConstants.NODESET);
            NodeList outputNodeList = (NodeList) outputResult;

            System.out.println("No of outputs: "+outputNodeList.getLength());

            for (int i = 0; i < outputNodeList.getLength(); i++) {
                String output = outputNodeList.item(i).getNodeValue();

                //System.out.println(output);

                outputs.add(output);
            }

            
        }
        catch (SAXParseException spe){
            session.setAttribute("status", "NO SUCH JOB");
        }
        catch (Exception e) {
            System.out.println("getOutputs excpetion: "+e);
        }
        //outputs.add("asdasdasd");

        return outputs;
    }
}
